package com.dscyy.readutil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DownEmailInfo {
	public static void main(String[] args) throws Exception {// 本程序内部异常过多为了简便，不一Try，直接抛给虚拟机
		Long StartTime = System.currentTimeMillis();

		String webaddress = "http://tieba.baidu.com/p/2314539885?pn=";// 创建输入对象
		File file = new File("D:" + File.separator + "downemails.txt");// 实例化文件类对象
		Set<String> allEmails = new HashSet<String>();;
		for(int i=2;i<31;i++) {
			String web = webaddress + i;
			allEmails.addAll(scrspyEmails(web));
		}
		allEmails.addAll(scrspyEmails("http://tieba.baidu.com/p/2314539885"));
		
		Writer outWriter = new FileWriter(file);// 实例化outWriter类
		
		for(String s : allEmails) {
			outWriter.write(s + "\r\n");// 将匹配的字符输入到目标文件
		}
		
		Long StopTime = System.currentTimeMillis();
		String UseTime = (StopTime - StartTime) + "";
		outWriter.write("--------------------------------------------------------\r\n");
		outWriter.write("本次爬取页面地址：" + webaddress + "\r\n");
		outWriter.write("爬取用时：" + UseTime + "毫秒\r\n");
		outWriter.write("本次共得到邮箱：" + allEmails.size() + "条\r\n");
		outWriter.write("****谢谢您的使用****\r\n");
		outWriter.write("--------------------------------------------------------");
		outWriter.close();// 关闭文件输出操作
		System.out.println(" —————————————————————\t");
		System.out.println("|页面爬取成功，请到D盘根目录下查看test文档|\t");
		System.out.println("|                                         |");
		System.out.println("|如需重新爬取，请再次执行程序,谢谢您的使用|\t");
		System.out.println(" —————————————————————\t");
	}

	private static Set<String> scrspyEmails(String webaddress) throws Exception {
		Set<String> data = new HashSet<String>();
		// 并指明输出地址和输出文件名
		URL url = new URL(webaddress);// 实例化URL类。
		URLConnection conn = url.openConnection();// 取得链接
		BufferedReader buff = new BufferedReader(new InputStreamReader(
				conn.getInputStream()));// 取得网页数据
		String line = null;
		String regex = "\\w+@\\w+(\\.\\w+)+";// 声明正则，提取网页前提
		Pattern p = Pattern.compile(regex);// 为patttern实例化
		while ((line = buff.readLine()) != null) {
			Matcher m = p.matcher(line);// 进行匹配
			while (m.find()) {
				data.add(m.group());// 将匹配的字符输入到目标文件
			}
		}
		buff.close();
		return data;
	}
	
}